def countBits(n):
    res = []
    for i in range(n + 1):
        temp = 0
        for one in '{:b}'.format(i):
            if one == '1':
                temp = temp + 1
        res.append(temp)
    return res


print(countBits(2))  # [0,0,1]
print(countBits(5))  # [0,1,1,2,1,2]
